package cn.uil.demo;

import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;

@Configuration
public class AsyncInitConfig {

    @Bean(name = "asyncInitExecutor")
    public Executor asyncInitExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(10);
        executor.setMaxPoolSize(20);
        executor.setQueueCapacity(100);
        executor.setThreadNamePrefix("AsyncInit-");
        executor.setRejectedExecutionHandler((r, e) -> {
            throw new AsyncInitException("Async init queue full");
        });
        executor.initialize();
        return executor;
    }

    // 全局异常处理
    @Bean
    public AsyncUncaughtExceptionHandler asyncUncaughtExceptionHandler() {
        return (ex, method, params) -> {
            System.err.println("Async init error in " + method.getName() + ": " + ex.getMessage());
        };
    }
}
